package com.jk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jk.model.*;
import com.jk.service.LXCService;
import com.jk.utils.OSSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("product")
public class LXCController {

    @Reference(check = false)
    private LXCService lxcService;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 获取登录账号
     * @param session
     * @return
     */
    @RequestMapping("checkLogin")
    @ResponseBody
    public Map checkLogin(HttpSession session){
        HashMap<String, Object> result = new HashMap<>();
        UserBean userBean = (UserBean) session.getAttribute(session.getId());
        if (userBean==null){
            result.put("code",1);
            result.put("msg","未登陆");
            return result;
        }
        result.put("code",2);
        result.put("msg","已登陆");
        result.put("userName",userBean.getUserAccount());
        return result;
    }

    /**
     * 商城导航
     * @return
     */
    @RequestMapping("findProductTree")
    @ResponseBody
    public List<ProductTree> findProductTree(){
        return lxcService.findProductTree();
    }

    /**
     * 新增商品
     * @param productBean
     * @return
     */
    @RequestMapping("addProduct")
    @ResponseBody
    public Boolean addProudct(ProductBean productBean,HttpSession session){
        try {
            UserBean attribute = (UserBean) session.getAttribute(session.getId());
            Integer storeId = attribute.getStoreId();
            lxcService.addProudct(productBean,storeId);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 商品分页查询
     * @param page
     * @param rows
     * @param productBean
     * @return
     */
    @RequestMapping("findProductList")
    @ResponseBody
    public Map findProduct(Integer page, Integer rows, ProductBean productBean,HttpSession session){
        UserBean attribute = (UserBean) session.getAttribute(session.getId());
        Integer userId = attribute.getId();
        return lxcService.findProductList(page,rows,productBean,userId);
    }

    /**
     * 商品上下架
     * @param productId
     * @param falg
     * @return
     */
    @RequestMapping("productState")
    @ResponseBody
    public Boolean productState(Integer productId,Integer falg){
        try {
            lxcService.productState(productId,falg);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 条查：加载类型
     * @return
     */
    @RequestMapping("findProductType")
    @ResponseBody
    public List<ProductTypeBean> findProductType(){
        return lxcService.findProductType();
    }

    /**
     * 删除商品
     * @param productId
     * @return
     */
    @RequestMapping("delProduct")
    @ResponseBody
    public Boolean delProduct(Integer productId){
        try {
            lxcService.delProduct(productId);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 库存商品列表
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("findProductStock")
    @ResponseBody
    public Map findProductStock(Integer page, Integer rows,ProductBean productBean,HttpSession session){
        UserBean attribute = (UserBean) session.getAttribute(session.getId());
        Integer userId = attribute.getId();
        return lxcService.findProductStock(page,rows,productBean,userId);
    }

    /**
     * 库存：商品批量上架
     * @param ids
     * @return
     */
    @RequestMapping("productManyStock")
    @ResponseBody
    public Boolean productManyStock(Integer[] ids){
        try {
            lxcService.productManyStock(ids);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 店铺：出售中的商品
     * @param page
     * @param rows
     * @param productBean
     * @return
     */
    @RequestMapping("findStoreProductList")
    @ResponseBody
    public Map findStoreProductList(Integer page, Integer rows,ProductBean productBean,HttpSession session){
        UserBean attribute = (UserBean) session.getAttribute(session.getId());
        Integer userId = attribute.getId();
        return lxcService.findStoreProductList(page,rows,productBean,userId);
    }

    /**
     * 店铺：下架的商品
     * @param page
     * @param rows
     * @param productBean
     * @return
     */
    @RequestMapping("findStoreDownProduct")
    @ResponseBody
    public Map findStoreDownProduct(Integer page, Integer rows,ProductBean productBean,HttpSession session){
        UserBean attribute = (UserBean) session.getAttribute(session.getId());
        Integer userId = attribute.getId();
        return lxcService.findStoreDownProduct(page,rows,productBean,userId);
    }

    /**
     * 审核中的商品
     * @param page
     * @param rows
     * @param productBean
     * @return
     */
    @RequestMapping("findAuditProduct")
    @ResponseBody
    public Map findAuditProduct(Integer page, Integer rows,ProductBean productBean,HttpSession session){
        UserBean attribute = (UserBean) session.getAttribute(session.getId());
        Integer userId = attribute.getId();
        return lxcService.findAuditProduct(page,rows,productBean,userId);
    }

    /**
     * 图片上传
     * @param imgfile
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("upload")
    @ResponseBody
    public Map upload(MultipartFile imgfile, HttpServletRequest request) throws IOException {
        OSSClientUtil ossClient = new OSSClientUtil();
        String name = ossClient.uploadImg2Oss(imgfile);
        String fileUpload = ossClient.getImgUrl(name);
        HashMap<String, Object> result = new HashMap<>();
        result.put("img", fileUpload);
        return result;
    }

    /**
     * 店铺查询
     * @return
     */
    @RequestMapping("findStore")
    @ResponseBody
    public List<StoreBean> findStore(){
        return lxcService.findStore();
    }

    @RequestMapping("findLogList")
    @ResponseBody
    public Map findLogList(Integer page, Integer rows){
        //构建总返回
        HashMap<String, Object> result = new HashMap<>();
        //查询方法
        Query query = new Query();
        //查询count
        long count = mongoTemplate.count(query , LogBean.class);
        query.skip((page-1)*rows);
        query.limit(rows);
        //查询list
        List<LogBean> find = mongoTemplate.find(query, LogBean.class);
        result.put("total", count);
        result.put("rows", find);
        return result;
    }


    /**
     * 登录
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public Map login(UserBean userBean, HttpServletRequest request){
        HashMap<String, Object> resyIt = new HashMap<>();
        UserBean userInfo= lxcService.findUserInfoByAccount(userBean.getUserAccount());
        if (userInfo == null) {
            resyIt.put("code", 2);
            resyIt.put("msg", "账号密码错误");
            return resyIt;
        }
        if (!userInfo.getUserPassword().equals(userBean.getUserPassword())) {
            resyIt.put("code", 3);
            resyIt.put("msg", "账号密码错误");
            return resyIt;
        }if(userInfo.getUserRole() !=2){
            resyIt.put("code", 4);
            resyIt.put("msg", "请登录商家账号");
            return resyIt;
        }
        resyIt.put("code", 0);
        resyIt.put("msg", "登陆成功");
        HttpSession session = request.getSession();
        session.setAttribute(session.getId(),userInfo);
        return resyIt;
    }


}
